﻿/*
#------------------------------------------------------------------------------
#-- Program Name:	[dbo].[spGetBackupDirectory]
#-- Purpose:		Gets the backup directory for a specific database.
#--	Last Update:	06/29/2011
#--					For a complete history - please review comments in Version
#--					control.
#-- Required XPs:	master.dbo.xp_instance_regread
#-- Called By:		[dbo].[spBackupFull]
#--					[dbo].[spBackupDifferential]
#--					[dbo].[spBackupLog]
#--					[dbo].[spGetFullBackupFileName]
#--					[dbo].[spGetDifferentialFileName]
#--					[dbo].[spGetLogFileName]
#------------------------------------------------------------------------------
*/
CREATE PROCEDURE [dbo].[spGetBackupDirectory]
(
	@database_name				sysname				= NULL,
	@backup_directory_name		varchar(8000)		OUTPUT
)
AS
BEGIN
	SELECT	@database_name = LTRIM(RTRIM(ISNULL(@database_name, '')))

	--- Get the configured backup directory from the registry
	EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'BackupDirectory', @backup_directory_name OUTPUT
	IF ISNULL(@backup_directory_name, '') = ''
	  BEGIN
		DECLARE @i int
		SET @i = 'A Backup Folder was not configured and is required (spSetBackupDirectory @backup_directory_name)' + 0
		RETURN (-999)
	  END
	ELSE
		SELECT	@backup_directory_name = LTRIM(RTRIM(@backup_directory_name))
		
	--- Ensure Backup Directory ends with a '\'
	IF NOT RIGHT(@backup_directory_name, 1) = '\'
		SELECT @backup_directory_name = @backup_directory_name + '\'

	-- Return the Database's Backup Directory
	IF @database_name <> ''
		SELECT	@backup_directory_name = @backup_directory_name + @database_name + '\'
END
